package com.zhangkunhao.notes.service;

import com.zhangkunhao.notes.dao.CollectQuestionDao;
import com.zhangkunhao.notes.entity.Do.CollectQuestion;
import com.zhangkunhao.notes.entity.Do.Question;
import com.zhangkunhao.notes.entity.bo.Message;
import com.zhangkunhao.notes.entity.bo.PageBean;
import com.zhangkunhao.notes.service.impl.CollectQuestionService;
import com.zhangkunhao.notes.utils.IfValidInputUtil;

import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 问题收藏夹接口的实现类
 *
 * @author 20642
 */
public class CollectQuestionServiceImpl implements CollectQuestionService {
    @Override
    public List<CollectQuestion> selectAll() {
        return CollectQuestionDao.getInstance().findAll();
    }

    @Override
    public void insert(CollectQuestion collectQuestion) throws Exception {
        CollectQuestionDao.getInstance().insert(collectQuestion);
    }

    @Override
    public int delete(int id) throws SQLException {
        return CollectQuestionDao.getInstance().delete(id);
    }

    @Override
    public int update(CollectQuestion collectQuestion, String... params) throws Exception {
        return CollectQuestionDao.getInstance().update(collectQuestion);
    }

    /**
     * 收藏问题
     *
     * @param userId：
     * @param question：
     * @return Message<Question>
     * @throws Exception:
     */
    public Message<Question> collectQuestion(int userId, Question question) throws Exception {
        //初始化
        Message<Question> questionMessage = new Message<>(null, "收藏该问题失败", 0);
        //判断
        if (question == null) {
            questionMessage.setResult("该问题为空，收藏失败");
            return questionMessage;
        } else if (!IfValidInputUtil.ifValidValue(question.getTitle())) {
            questionMessage.setResult("该问题标题为空，收藏失败");
            return questionMessage;
        }
        //判断是否已经收藏了
        boolean ifEverCollect = this.selectAll().stream()
                .anyMatch(collectQuestion -> collectQuestion.getUserId().equals(userId) && collectQuestion.getQuestionId().equals(question.getId()));
        //已经收藏
        if (ifEverCollect) {
            questionMessage.setResult("该问题已经收藏，收藏失败");
            return questionMessage;
        }

        //调用dao
        this.insert(new CollectQuestion(null, userId, question.getId(), question.getTitle()));

        //提供判断
        boolean anyMatch = this.selectAll().stream()
                .anyMatch(collectQuestion -> collectQuestion.getQuestionId().equals(question.getId()));

        //成功
        if (anyMatch) {
            questionMessage.setResult("收藏该问题成功，请到我的问题收藏夹里面查看");
            questionMessage.setStatus(1);
            questionMessage.setT(null);
        }
        //返回数据
        return questionMessage;
    }

    /**
     * 展示问题详情
     *
     * @param showQuestionId：
     * @return Message<Question>
     */
    public Message<Question> showQuestionDetails(int showQuestionId) {
        //调用dao
        Question question = new QuestionServiceImpl().selectAll().stream()
                .filter(question1 -> question1.getId().equals(showQuestionId))
                .collect(Collectors.toList()).get(0);
        //返回数据
        return new Message<>(question, "查看问题成功", 1);
    }

    /**
     * 显示我收藏的问题列表
     *
     * @param userId：
     * @return PageBean<CollectQuestion>
     */
    public PageBean<CollectQuestion> showMyCollectQuestions(int userId) {
        //查询我已经收藏的问题
        List<CollectQuestion> collectQuestions = this.selectAll().stream()
                .filter(collectQuestion -> collectQuestion.getUserId().equals(userId))
                .collect(Collectors.toList());

        //返回数据
        return new PageBean<>(collectQuestions.size(), collectQuestions);
    }
}
